<script lang="ts" setup name="XtxCity">
import { reactive, ref, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { Cites, CityResult } from '@/types/data'

// 父传子  默认地址
const { address = '' } = defineProps<{
  address: string
}>()
// const emit = defineEmits(['changeCity'])
const emit = defineEmits<{
  (e: 'changeCity', value: CityResult): void
}>()
//控制 下拉框是否显示  active
const active = ref(false)

const target = ref(null)

// 点击下拉框外边 隐藏  (1,事件冒泡  2.vueuse/core   =>onClickOutside)
// active.value = false
onClickOutside(target, _ => (active.value = false))

// 城市地址: https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json
// const cites = ref([] as Cites[])
// 城市列表
const cites = ref<Cites[]>([])
const cacheCites = ref([] as Cites[])

const changeResult: CityResult = reactive({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})
const getCites = async () => {
  // if (cites.value.length) return
  const res = await fetch('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  const data = await res.json()
  console.log(res)
  console.log(data)
  cites.value = data
  cacheCites.value = data
}

// getCites()   // 点击选择城市在获取城市列表

const selectCity = (val: Cites) => {
  // console.log(val)
  // 点击 把城市传过来 然后更新下来框内容, 并且隐藏下拉框
  // 这三元  用多重if更好理解
  val.level === 2
    ? ((active.value = false), (changeResult.countyCode = val.code), (changeResult.countyName = val.name), emit('changeCity', changeResult))
    : val.level === 1
    ? ((cites.value = val.areaList), (changeResult.cityCode = val.code), (changeResult.cityName = val.name))
    : ((cites.value = val.areaList), (changeResult.provinceCode = val.code), (changeResult.provinceName = val.name))
}

// 监听关闭弹窗的处理，恢复数据
watch(active, value => {
  // 当关闭active的时候，需要回复数据
  if (!value) {
    cites.value = cacheCites.value
  }
})
</script>

<template>
  <div class="xtx-city" ref="target">
    <div class="select" :class="{ active }" @click="active = !active">
      <!-- 下面俩标签互斥 -->
      <span v-if="!address" class="placeholder" @click="getCites">请选择配送地址</span>
      <span v-else class="value">{{ address }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" :class="{ active }" v-show="active">
      <span class="ellipsis" v-for="item in cites" :key="item.code" @click="selectCity(item)">{{ item.name }}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    // background-color: pink;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    opacity: 0.5;
    transition: all 0.5s linear;
    min-height: 0;

    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
    &.active {
      min-height: 30px;
      opacity: 1;
    }
  }
}
</style>
